package com.hangim.pce.service.impl;

import com.hangim.pce.common.service.impl.BaseEntityServiceImpl;
import com.hangim.pce.mapper.SysDrugUsageRuleMapper;
import com.hangim.pce.model.SysDrugUsageRule;
import com.hangim.pce.service.SysDrugUsageRuleService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pig.common.util.StringUtils;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 药品用量规则表 服务实现类
 * </p>
 *
 * @author hongjiliang
 * @since 2018-10-19
 */
@Service
public class SysDrugUsageRuleServiceImpl extends BaseEntityServiceImpl<SysDrugUsageRuleMapper, SysDrugUsageRule> implements SysDrugUsageRuleService {

    @Override
    public List<SysDrugUsageRule> getDrugUsageRulesByName(String name) {
        return mapper.selectList(new EntityWrapper<SysDrugUsageRule>().eq("drug_name", name));
    }

    @Override
    public List<SysDrugUsageRule> getDrugUsageRulesByDrugId(Long drugId) {
        List<SysDrugUsageRule> rules = mapper.selectList(new EntityWrapper<SysDrugUsageRule>().eq("drug_id", drugId));
        for (SysDrugUsageRule rule : rules) {
            List<Integer> usages = Lists.newArrayList();
            if (StringUtils.isNotEmpty(rule.getUsage())) {
                String[] strings = rule.getUsage().split(";");
                for (String string : strings) {
                    usages.add(Integer.parseInt(string));
                }
            }
            rule.setUsages(usages);
        }
        return rules;
    }
}
